home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* Rendering tutorial using GLUT library
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- /* Function Prototypes */
- void progInit( char * );
- void drawScene( void );
- void resize( int, int );
- void mouse( int, int, int, int );
- void motion( int, int );
- void colorMenu( int);
- void shapeMenu( int);
- void mainMenu( int);
- void drawCrossHairs( void);
- void markPoints(void);
- void renderString( const char *s);
- void printStatus( void);
-
-
- typedef struct {
- GLfloat color[3];
- GLint coords[2];
- } Point;
-
- enum Color { RED = 1, GREEN, BLUE, WHITE };
-
- static Point points[1000];
- static int winW, winH, mouseX, mouseY, numPts = 0;
- static int currColor = RED;
- static GLenum currMode = GL_POINTS;
-
- void
- main( int argc, char *argv[] )
- {
- glutInit( &argc, argv);
- progInit( argv[0] );
- glutMainLoop();
- }
-
- void
- progInit( char *progname )
- {
- int colorMenuID, shapeMenuID;
-
- glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
- glutInitWindowSize( 800, 600);
- glutCreateWindow( progname );
- glutDisplayFunc( drawScene );
- glutReshapeFunc( resize );
- glutMouseFunc( mouse );
- glutPassiveMotionFunc( motion );
-
- colorMenuID = glutCreateMenu(colorMenu);
- glutAddMenuEntry( "Red", RED);
- glutAddMenuEntry( "Green", GREEN);
- glutAddMenuEntry( "Blue", BLUE);
- glutAddMenuEntry( "White", WHITE);
-
- shapeMenuID = glutCreateMenu(shapeMenu);
- glutAddMenuEntry( "Points", 1);
- glutAddMenuEntry( "Lines", 2);
- glutAddMenuEntry( "Line strip", 3);
- glutAddMenuEntry( "Line loop", 4);
- glutAddMenuEntry( "Polygon", 5);
- glutAddMenuEntry( "Triangles", 6);
- glutAddMenuEntry( "Quads", 7);
- glutAddMenuEntry( "Triangle strip", 8);
- glutAddMenuEntry( "Triangle fan", 9);
- glutAddMenuEntry( "Quad strip", 10);
-
- glutCreateMenu(mainMenu);
- glutAddSubMenu( "Set current color", colorMenuID);
- glutAddSubMenu( "Generate primitive", shapeMenuID);
- glutAddMenuEntry( "Start over", 1);
- glutAddMenuEntry( "Undo last vertex", 2);
- glutAddMenuEntry( "Use flat shading", 3);
- glutAddMenuEntry( "Exit", 1000);
- glutAttachMenu(GLUT_RIGHT_BUTTON);
-
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- glPointSize(3.0);
- glLineWidth(2.0);
-
- /* For accurate rasterization */
- glTranslatef(0.375, 0.375, 0.0);
- }
-
- void resize( int w, int h )
- {
- winW = w;
- winH = h;
-
- glViewport(0, 0, w, h);
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D( 0, w, 0, h);
- glMatrixMode(GL_MODELVIEW);
- }
-
-
- void mouse( int button, int state, int x, int y )
- {
- if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN)
- return;
-
- switch(currColor) {
- case RED:
- points[numPts].color[0] = 1.0;
- points[numPts].color[1] = 0.0;
- points[numPts].color[2] = 0.0;
- break;
- case GREEN:
- points[numPts].color[0] = 0.0;
- points[numPts].color[1] = 1.0;
- points[numPts].color[2] = 0.0;
- break;
- case BLUE:
- points[numPts].color[0] = 0.0;
- points[numPts].color[1] = 0.0;
- points[numPts].color[2] = 1.0;
- break;
- case WHITE:
- points[numPts].color[0] = 1.0;
- points[numPts].color[1] = 1.0;
- points[numPts].color[2] = 1.0;
- break;
- }
- points[numPts].coords[0] = x;
- points[numPts].coords[1] = winH - y;
- numPts++;
-
- glutPostRedisplay();
- }
-
- void motion( int x, int y )
- {
- mouseX = x;
- mouseY = winH - y;
-
- glutPostRedisplay();
- }
-
-
- void colorMenu( int value)
- {
- currColor = value;
- glutPostRedisplay();
- }
-
- void shapeMenu( int value)
- {
- switch(value) {
- case 1: currMode = GL_POINTS; break;
- case 2: currMode = GL_LINES; break;
- case 3: currMode = GL_LINE_STRIP; break;
- case 4: currMode = GL_LINE_LOOP; break;
- case 5: currMode = GL_POLYGON; break;
- case 6: currMode = GL_TRIANGLES; break;
- case 7: currMode = GL_QUADS; break;
- case 8: currMode = GL_TRIANGLE_STRIP; break;
- case 9: currMode = GL_TRIANGLE_FAN; break;
- case 10: currMode = GL_QUAD_STRIP; break;
- default: currMode = GL_POINTS; break;
- }
- glutPostRedisplay();
- }
-
- void mainMenu( int value)
- {
- int numItems = glutGet(GLUT_MENU_NUM_ITEMS);
-
- switch(value) {
- case 1: numPts = 0; break;
- case 2: numPts--; break;
- case 3:
- glShadeModel(GL_FLAT);
- glutChangeToMenuEntry(numItems - 1,
- "Use smooth shading",
- 4);
- break;
- case 4:
- glShadeModel(GL_SMOOTH);
- glutChangeToMenuEntry(numItems - 1,
- "Use flat shading",
- 3);
- break;
- case 1000:
- exit(0);
- break;
-
- default: break;
- }
- glutPostRedisplay();
- }
-
- #if 0
- void drawGrid( void)
- {
- int i, j;
- GLfloat grey[] = { 0.6, 0.6, 0.6 };
-
- glPushAttrib(GL_CURRENT_BIT | GL_LINE_BIT);
- glColor3fv(grey);
- glLineWidth(1.0);
- glBegin(GL_LINES);
-
- for (i = 0; i < winW; i += 30) {
- glVertex2i(i, 0);
- glVertex2i(i, winH);
- }
- for (j = 0; j < winH; j += 30) {
- glVertex2i(0, j);
- glVertex2i(winW, j);
- }
- glEnd();
- glPopAttrib();
- }
- #endif
-
- void drawCrossHairs( void)
- {
- GLfloat red[] = { 1.0, 0.0, 0.0 };
-
- glPushAttrib(GL_ALL_ATTRIB_BITS);
-
- glEnable(GL_BLEND);
- glBlendEquationEXT(GL_LOGIC_OP);
- glLogicOp(GL_XOR);
-
- glColor3fv(red);
- glLineWidth(1.0);
- glBegin(GL_LINES);
- glVertex2i(0, mouseY);
- glVertex2i(winW, mouseY);
- glVertex2i(mouseX, 0);
- glVertex2i(mouseX, winH);
- glEnd();
-
- glPopAttrib();
- }
-
- void markPoints( void)
- {
- int i, x, y;
- char str[5];
-
- glPushAttrib(GL_ALL_ATTRIB_BITS);
-
- glEnable(GL_BLEND);
- glBlendEquationEXT(GL_LOGIC_OP);
- glLogicOp(GL_XOR);
-
- /* Label each vertex */
-
- for (i = 0; i < numPts; i++) {
- glColor3fv(points[i].color);
- x = points[i].coords[0];
- y = points[i].coords[1];
- glRasterPos2i(x - 26, y - 4);
- sprintf(str, "v%d", i);
- renderString(str);
- }
-
- /* If not drawing points, put a little 'x' at each vertex */
-
- if (currMode != GL_POINTS) {
- glLineWidth(1.5);
- glBegin(GL_LINES);
- for (i = 0; i < numPts; i++) {
- glColor3fv(points[i].color);
- x = points[i].coords[0];
- y = points[i].coords[1];
- glVertex2i(x - 5, y - 5);
- glVertex2i(x + 5, y + 5);
- glVertex2i(x - 5, y + 5);
- glVertex2i(x + 5, y - 5);
- }
- glEnd();
- }
- glPopAttrib();
- }
-
- void renderString( const char *s)
- {
- while (*s != '\0')
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *s++);
- }
-
-
- void printStatus( void)
- {
- GLint shadeModel;
-
- glPushAttrib(GL_CURRENT_BIT | GL_LINE_BIT);
-
- glColor3f(1.0, 1.0, 0.0);
- glRasterPos2f(5.0, 35.0);
- renderString( "Drawing ");
- switch(currMode) {
- case GL_POINTS:
- renderString("GL_POINTS"); break;
- case GL_LINES:
- renderString("GL_LINES"); break;
- case GL_LINE_STRIP:
- renderString("GL_LINE_STRIP"); break;
- case GL_LINE_LOOP:
- renderString("GL_LINE_LOOP"); break;
- case GL_POLYGON:
- renderString("GL_POLYGON"); break;
- case GL_TRIANGLES:
- renderString("GL_TRIANGLES"); break;
- case GL_QUADS:
- renderString("GL_QUADS"); break;
- case GL_TRIANGLE_STRIP:
- renderString("GL_TRIANGLE_STRIP"); break;
- case GL_TRIANGLE_FAN:
- renderString("GL_TRIANGLE_FAN"); break;
- case GL_QUAD_STRIP:
- renderString("GL_QUAD_STRIP"); break;
- default: break;
- }
-
- glRasterPos2f(5.0, 20.0);
- renderString( "Current color is ");
- switch(currColor) {
- case RED: renderString("red"); break;
- case GREEN: renderString("green"); break;
- case BLUE: renderString("blue"); break;
- case WHITE: renderString("white"); break;
- default: break;
- }
- glRasterPos2f(5.0, 5.0);
- glGetIntegerv(GL_SHADE_MODEL, &shadeModel);
- switch(shadeModel) {
- case GL_FLAT: renderString("Flat shaded"); break;
- case GL_SMOOTH: renderString("Smooth shaded"); break;
- default: break;
- }
- glColor3f(1.0, 1.0, 1.0);
- glRasterPos2f(winW - 313, 20.0);
- renderString( "Left mouse button creates vertex");
- glRasterPos2f(winW - 313, 5.0);
- renderString( "Right mouse button pops up menu");
- glPopAttrib();
- }
-
-
- GLvoid
- drawScene( void )
- {
- int i;
-
- glClear( GL_COLOR_BUFFER_BIT );
-
- glBegin(currMode);
- for (i = 0; i < numPts; i++) {
- glColor3fv(points[i].color);
- glVertex2iv(points[i].coords);
- }
- glEnd();
-
- drawCrossHairs();
- markPoints();
- printStatus();
-
- glutSwapBuffers();
- }
-